﻿
var gulp = require('gulp');
var less = require('gulp-less');
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
var clean = require('gulp-clean');
var usemin = require('gulp-usemin');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var cleanCss = require('gulp-clean-css');
var zip = require('gulp-zip')
var runSequence = require('gulp-sequence');
var rev = require('gulp-rev');
var _ = require('lodash');
var Promise = require('bluebird');
var buildBasePath = 'angular/';//构建输出的目录
var copyFile = function (_src, _dest) {
    return new Promise(function (_resolve) {
        gulp.src(_src).pipe(gulp.dest(_dest)).on('end', _resolve);
    });
};
var data = {
    libs: {
        files: [
            {
                src: [
                    'angular/angular.js',
                    'angular-animate/angular-animate.js',
                    'angular-aria/angular-aria.js',
                    'angular-cookies/angular-cookies.js',
                    'angular-messages/angular-messages.js',
                    'angular-resource/angular-resource.js',
                    'angular-sanitize/angular-sanitize.js',
                    'angular-touch/angular-touch.js',
                    'angular-bootstrap/ui-bootstrap-tpls.js',
                    'angular-bootstrap-nav-tree/dist/**',
                    'angular-file-upload/angular-file-upload.js',
                    'angular-loading-bar/build/**',
                    'angular-material/angular-material.js',
                    'angular-material/angular-material.css',
                    'angular-smart-table/dist/**',
                    'angular-translate/angular-translate.js',
                    'angular-translate-loader-static-files/angular-translate-loader-static-files.js',
                    'angular-translate-storage-cookie/angular-translate-storage-cookie.js',
                    'angular-translate-storage-local/angular-translate-storage-local.js',
                    'angular-ui-grid/ui-grid.*',
                    'angular-ui-calendar/src/calendar.js',
                    'angular-ui-map/ui-map.js',
                    'angular-ui-router/release/**',
                    'angular-ui-select/dist/**',
                    'angular-ui-utils/ui-utils.js',
                    'angular-xeditable/dist/**',
                    'angularjs-toaster/toaster.js',
                    'angularjs-toaster/toaster.css',
                    'angular-skycons/angular-skycons.min.js',
                    'jquery.easy-pie-chart/dist/angular.easypiechart.js',
                    'ng-grid/build/**',
                    'ng-grid/ng-grid.min.css',
                    'ngImgCrop/compile/minified/**',
                    'ngstorage/ngStorage.js',
                    'oclazyload/dist/**',
                    'textAngular/dist/**',
                    'venturocket-angular-slider/build/**',
                    'videogular/videogular.min.js',
                    'videogular-controls/controls.min.js',
                    'videogular-buffering/buffering.min.js',
                    'videogular-overlay-play/overlay-play.min.js',
                    'videogular-poster/poster.min.js',
                    'videogular-ima-ads/ima-ads.min.js'
                ],
                dest: 'libs/angular',
                cwd: 'bower_components',
                expand: true
            },
            {
                src: [
                    'jquery/dist/jquery.js',
                    'bootstrap/dist/**',
                    'datatables/media/js/jquery.dataTables.min.js',
                    'plugins/integration/bootstrap/3/**',
                    'plugins/integration/bootstrap/images/**',
                    'footable/dist/footable.all.min.js',
                    'footable/css/footable.core.css',
                    'footable/css/fonts/**',
                    'bower-jvectormap/*.js',
                    'flot/jquery.flot.js',
                    'flot/jquery.flot.resize.js',
                    'flot/jquery.flot.pie.js',
                    'flot.tooltip/js/jquery.flot.tooltip.min.js',
                    'flot-spline/js/jquery.flot.spline.min.js',
                    'flot.orderbars/js/jquery.flot.orderBars.js',
                    'moment/moment.js',
                    'screenfull/dist/screenfull.min.js',
                    'slimscroll/jquery.slimscroll.min.js',
                    'html5sortable/jquery.sortable.js',
                    'nestable/jquery.nestable.js',
                    'bootstrap-filestyle/src/bootstrap-filestyle.js',
                    'bootstrap-slider/bootstrap-slider.js',
                    'bootstrap-slider/bootstrap-slider.css',
                    'chosen/chosen.jquery.min.js',
                    'bootstrap-touchspin/dist/jquery.bootstrap-touchspin.min.js',
                    'bootstrap-touchspin/dist/jquery.bootstrap-touchspin.min.css',
                    'bootstrap-wysiwyg/bootstrap-wysiwyg.js',
                    'bootstrap-wysiwyg/external/jquery.hotkeys.js',
                    'fullcalendar/dist/fullcalendar.min.js',
                    'bootstrap-daterangepicker/daterangepicker.js',
                    'bootstrap-daterangepicker/daterangepicker-bs3.css',
                    'bootstrap-tagsinput/dist/bootstrap-tagsinput.js',
                    'bootstrap-tagsinput/dist/bootstrap-tagsinput.css',
                    'jquery_appear/jquery.appear.js'
                ],
                dest: 'libs/jquery',
                cwd: 'bower_components',
                expand: true
            },
            {
                src: [
                    'animate.css/animate.css',
                    'font-awesome/css/**',
                    'font-awesome/fonts/**',
                    'simple-line-icons/css/**',
                    'simple-line-icons/fonts/**',
                    'bootstrap-rtl/dist/css/bootstrap-rtl.min.css'
                ],
                dest: 'libs/assets',
                cwd: 'bower_components',
                expand: true
            },
            {
                src: '**',
                cwd: 'bower_components/bootstrap/dist/fonts',
                dest: 'src/fonts',
                expand: true
            },
            {
                src: 'lodash.min.js',
                cwd: 'node_modules/lodash',
                dest: 'libs/assets/lodash',
                expand: true
            }
        ]
    },
    angular: {
        files: [
            { expand: true, src: ['**', '!**/less/**'], cwd: 'src/', dest: "angular/" }
        ]
    },
    html: {
        files: [
            { expand: true, src: '**', cwd: 'src/fonts/', dest: 'html/fonts/' },
            { expand: true, src: "**", cwd: 'src/api', dest: "html/api" },
            { expand: true, src: '**', cwd: 'src/img/', dest: 'html/img/' },
            { expand: true, src: '*.css', cwd: 'src/css/', dest: 'html/css/' },
            { expand: true, src: '**', cwd: 'swig/js/', dest: 'html/js/' }
        ]
    },
    landing: {
        files: [
            { expand: true, src: '**', cwd: 'src/fonts/', dest: 'landing/fonts/' },
            { expand: true, src: '*.css', cwd: 'src/css/', dest: 'landing/css/' },
            { src: 'html/css/app.min.css', dest: 'landing/css/app.min.css' }
        ]
    }
};
var bAngular = _.map(data.libs.files[0].src, function (i) {
    return i = data.libs.files[0].cwd + '/' + i;
});
var bJquery = _.map(data.libs.files[1].src, function (i) {
    return i = data.libs.files[1].cwd + '/' + i;
});
var bAssets = _.map(data.libs.files[2].src, function (i) {
    return i = data.libs.files[2].cwd + '/' + i;
});
// 静态服务器 + 监听 * 文件
gulp.task('serve', function () {
    browserSync.init({
        server: {
            baseDir: "./"
        },
        port: 3006
    });
    gulp.watch("src/**/*.json").on('change', reload);
    gulp.watch("src/**/*.js").on('change', reload);
    gulp.watch("src/**/*.html").on('change', reload);
    gulp.watch(["src/**/*.css", "libs/**/*.css"]).on('change', function (e) {
        gulp.src(e.path).pipe(browserSync.stream());
    });
    gulp.watch("src/css/weui/**/*.less").on('change', function (e) {
        gulp.src('src/css/weui/weui-diy.less')
            .pipe(less())
            .pipe(gulp.dest('src/css/'));
    });
});

// 清空生成环境
gulp.task('clean', function () {
    return gulp.src(buildBasePath, { read: false })
         .pipe(clean());
});

// 拷贝将 开发环境(src)代码 拷贝至 生成环境(angular)
gulp.task('copy', function (_callback) {
    copyFile(['src/**', '!**/less/**', '!**/weui/**'], buildBasePath)
        .then(_callback);
});

// 拷贝 js/css 库
gulp.task('copylibs', function (_callback) {
    Promise.resolve()
        .then(function () {
            return copyFile(bAngular, 'libs/angular');
        })
        .then(function () {
            return copyFile(bJquery, 'libs/jquery');
        })
        .then(function () {
            return copyFile(bAssets, 'libs/assets');
        })
        .then(_callback);
});

// 压缩 index.html 文件
gulp.task('usemin', function () {
    return gulp.src('src/index.html')
        .pipe(usemin({
            css: [cleanCss(), rev()],
            js: [uglify(), rev()]
        }))
        .pipe(gulp.dest(buildBasePath));
});

// 编译 less
gulp.task('less', function (_callback) {
    gulp.src([
            'src/css/less/app.less',
            'src/css/less/bootstrap.utils.less',
            'src/css/weui/weui-diy.less'])
        .pipe(less())
        .pipe(gulp.dest('src/css'))
        .on('end', _callback);
});

// 打包 .zip 文件(全量打包)
gulp.task('zip:full', function () {
    var copyFile = function (_pathName) {
        return function () {
            return new Promise(function (_resolve, _reject) {
                gulp
                    .src([_pathName + '/**'])
                    .pipe(gulp.dest('.tmp/mobileWeb/' + _pathName))
                    .on('end', function (_err) {
                        _err ? _reject(_err) : _resolve();
                    });
            });
        }
    };
    Promise
        .resolve()
        .then(copyFile('angular'))
        .then(copyFile('libs'))
        .then(function () {
            return new Promise(function (_resolve, _reject) {
                gulp.src('.tmp/mobileWeb/**')
                    .pipe(zip('STTXMobileWeb-full.zip'))
                    .pipe(gulp.dest('D:/CXP.Document'))
                    .on('end', function (_err) {
                        _err ? _reject(_err) : _resolve();
                    })

            });
        })
        .then(function () {
            gulp.src('.tmp', { read: false })
                .pipe(clean());
        });
})

// 打包 .zip 文件
gulp.task('zip', function () {
    var copyFile = function (_pathName) {
        return function () {
            return new Promise(function (_resolve, _reject) {
                gulp
                    .src([_pathName + '/**'])
                    .pipe(gulp.dest('.tmp/mobileWeb/' + _pathName))
                    .on('end', function (_err) {
                        _err ? _reject(_err) : _resolve();
                    });
            });
        }
    };
    Promise
        .resolve()
        .then(copyFile('angular'))
        .then(function () {
            return new Promise(function (_resolve, _reject) {
                gulp.src('.tmp/mobileWeb/**')
                    .pipe(zip('STTXMobileWeb.zip'))
                    .pipe(gulp.dest('D:/CXP.Document'))
                    .on('end', function (_err) {
                        _err ? _reject(_err) : _resolve();
                    })

            });
        })
        .then(function () {
            gulp.src('.tmp', { read: false })
                .pipe(clean());
        });
});

// 生成 angular
gulp.task('build', function (callback) {
    runSequence('less', 'clean', 'copylibs', 'copy', 'usemin', callback);
});

// 生成 angular, 并拷贝至 P 盘
gulp.task('copyng2', function (_callback) {
    var ng2Path = 'P:/Projects/HXKJ/RMSCG/STTXPT/MMB/MMBWeChat/ng2';

    runSequence('build:angular', function () {
        Promise.resolve()
            .then(function () {
                return copyFile('libs/**/*', ng2Path + '/libs');
            })
            .then(function () {
                return copyFile('angular/**/*', ng2Path + '/angular');
            })
            .then(_callback);
    });

});

gulp.task('default', ['serve']);





